#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
using namespace std;


class String
{
public:
    String(const char *str = NULL);	//通用构造函数
    String(const String &str);		//拷贝构造函数
    ~String();						//析构函数

    friend  String operator+(const String &s1, const String &s2);
    friend String operator+(const String &s1, const char *s2);
    // String operator+(const String &str) const;	//重载+
    //    String operator+(const char *str) ;
    String& operator=(const String &str);		//重载=
    String& operator+=(const String &str);		//重载+=
    bool operator==(const String &str) const;	//重载==
    char& operator[](int n) const;				//重载[]

    size_t size() const;		//获取长度
    const char* c_str() const;	//获取C字符串

    friend istream& operator>>(istream &is, String &str);//输入
    friend ostream& operator<<(ostream &os, String &str);//输出
    //    friend ostream& operator<<(ostream &os, const char *str);//输出
private:
    char *data;		//字符串
    size_t length;	//长度
};

String::String(const char *str)//通用构造函数
{
    if (!str)
    {
        length = 0;
        data = new char[1];
        *data = '\0';
    }
    else
    {
        length = strlen(str);
        data = new char[length + 1];
        strcpy(data, str);
    }
}

String::String(const String &str)//拷贝构造函数
{
    length = str.size();
    data = new char[length + 1];
    strcpy(data, str.c_str());
}

String::~String()//析构函数
{
    delete []data;
    length = 0;
}

String operator+(const String &s1, const String &s2)
{
    //int len = strlen(s1.str_) + strlen(s2.str_) + 1;
    //char* newstr = new char[len];
    //memset(newstr, 0, len);
    //strcpy(newstr, s1.str_);
    //strcat(newstr, s2.str_);
    //
    //String tmp(newstr);
    //delete newstr;
    String str = s1;
    str += s2;
    return str;
}

String operator+(const String &s1, const char *s2)
{
    //int len = strlen(s1.str_) + strlen(s2.str_) + 1;
    //char* newstr = new char[len];
    //memset(newstr, 0, len);
    //strcpy(newstr, s1.str_);
    //strcat(newstr, s2.str_);
    //
    //String tmp(newstr);
    //delete newstr;
    String str = s1;
    String str2(s2);
    str += str2;
    return str;
}

//String String::operator+(const String &str) const//重载+
//{
//    String newString;                           // 这个是在栈上分配的，不能使用返回引用的方式，只能返回值

//    newString.length = length + str.size();
//    newString.data = new char[newString.length + 1];
//    strcpy(newString.data, data);
//    strcat(newString.data, str.data);

//    return newString;
//}

//String operator+(const char *str)
//{
//    String newString;                           // 这个是在栈上分配的，不能使用返回引用的方式，只能返回值

//    newString.length = length + strlen(str);
//    newString.data = new char[newString.length + 1];
//    strcpy(newString.data, data);
//    strcat(newString.data, str);

//    return newString;
//}

String& String::operator=(const String &str)//重载=
{
    if (this == &str)
        return *this;

    delete []data;
    length = str.length;
    data = new char[length + 1];
    strcpy(data, str.c_str());
    return *this;
}

String& String::operator+=(const String &str)//重载+=
{
    length += str.length;
    char *newData = new char[length + 1];
    strcpy(newData, data);
    strcat(newData, str.data);
    delete []data;
    data = newData;
    return *this;
}

inline bool String::operator==(const String &str) const//重载==
{
    if (length != str.length)
        return false;
    return strcmp(data, str.data) ? false : true;
}

inline char& String::operator[](int n) const//重载[]
{
    if (n >= length)
        return data[length-1]; //错误处理
    else
        return data[n];
}

inline size_t String::size() const//获取长度
{
    return length;
}

istream& operator>>(istream &is, String &str)//输入
{
    char tem[1000];  //简单的申请一块内存
    is >> tem;      // 先把数据写入到缓冲区
    str.length = strlen(tem);
    str.data = new char[str.length + 1];
    strcpy(str.data, tem);
    return is;
}

ostream& operator<<(ostream &os, String &str)//输出
{
    //std::cout << "-->os << str = " << str.c_str() << std::endl;
    os << str.data;
    return os;
}

//ostream& operator<<(ostream &os, const char *str)//输出
//{
//    //std::cout << "-->os << str = " << str << std::endl;
//    os << str;
//    return os;
//}

inline const char* String::c_str() const//获取C字符串
{
    return data;
}





int main()
{
    String s;
    std::cout << "String test start" << std::endl;
    std::cout << "Please input a string" << std::endl;
    std::cin >> s;
    std::cout << s << ": " << s.size() << std::endl;

    char a[] = "Hello";
    char b[] = "World!";
    String s1(a), s2(b);
    String s4 = s1 + s2 + " lily zhang";
    //std::cout << s1 << " + " << (s2  + " qingfu ") << " = " << s4  << std::endl;
    std::string liao("qingfu");
    std::cout << (s2 + "need this").c_str() << std::endl;
    String s3 = s1 + s2;
    if (s1 == s3)
        std::cout << "First: s1 == s3" << std::endl;

    s1 += s2;
    s1 += s;
    if (s1 == s3)	std::cout << "Second: s1 == s3" << std::endl;
    std::cout << s1 << std::endl;


    std::cout << "String test finish" << std::endl;
    system("pause");
    return 0;
}



void SrsThread::thread_cycle()
{
    int ret = ERROR_SUCCESS;

    _srs_context->generate_id();
    srs_info("thread %s cycle start", _name);

    _cid = _srs_context->get_id();

    srs_assert(handler);
    handler->on_thread_start();

    // thread is running now.
    really_terminated = false;

    // wait for cid to ready, for parent thread to get the cid.
    while (!can_run && loop) {
        st_usleep(10 * 1000);
    }

    while (loop) {
        if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) {
            srs_warn("thread %s on before cycle failed, ignored and retry, ret=%d", _name, ret);
            goto failed;
        }
        srs_info("thread %s on before cycle success", _name);
        // 注意纯虚函数的应用
        if ((ret = handler->cycle()) != ERROR_SUCCESS) {
            if (!srs_is_client_gracefully_close(ret) && !srs_is_system_control_error(ret)) {
                srs_warn("thread %s cycle failed, ignored and retry, ret=%d", _name, ret);
            }
            goto failed;
        }
        srs_info("thread %s cycle success", _name);

        if ((ret = handler->on_end_cycle()) != ERROR_SUCCESS) {
            srs_warn("thread %s on end cycle failed, ignored and retry, ret=%d", _name, ret);
            goto failed;
        }
        srs_info("thread %s on end cycle success", _name);

failed:
        if (!loop) {
            break;
        }

        // to improve performance, donot sleep when interval is zero.
        // @see: https://github.com/ossrs/srs/issues/237
        if (cycle_interval_us != 0) {
            st_usleep(cycle_interval_us);
        }
    }

    // readly terminated now.
    really_terminated = true;

    handler->on_thread_stop();
    srs_info("thread %s cycle finished", _name);
}
